package net.luoran.dvm.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.luoran.dvm.common.base.PageFactory;
import net.luoran.dvm.common.base.Result;
import net.luoran.dvm.entity.SysUserDO;
import net.luoran.dvm.entity.dto.SysUserDTO;
import net.luoran.dvm.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author MyBatisPlusGenerater
 * @since 2020-10-29
 */
@Controller
@RequestMapping("/user")
public class SysUserController {

    @Value("${server.servlet.context-path}")
    public String contextPath;

    @GetMapping
    public String sysUser(Model model) {
        model.addAttribute("contextPath", contextPath);
        return "user";
    }

    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询展示
     *
     * @param sysUserDTO
     * @return
     */
    @PostMapping("/queryList")
    @ResponseBody
    public Result showList(@RequestBody SysUserDTO sysUserDTO) {
        Result result = Result.ok();
        try {
            QueryWrapper<SysUserDO> queryWrapper = new QueryWrapper();
            if (StringUtils.isNotBlank(sysUserDTO.getUserName())) {
                queryWrapper.like("user_name", sysUserDTO.getUserName());
            }
            Page<SysUserDO> page = sysUserService.page(PageFactory.createPageInfo(sysUserDTO.getCurrent(), sysUserDTO.getSize()), queryWrapper);
            result.setData(page);
            result.setDraw(sysUserDTO.getDraw() + 1);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("系统错误");
        }
        return result;
    }

    /**
     * 批量删除用户的信息
     *
     * @param userIdList
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody List<Integer> userIdList) {
        Result result = Result.ok();
        try {
            if (userIdList.size() > 0) {
                sysUserService.removeByIds(userIdList);
            } else {
                return Result.error("不能为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("系统错误");
        }
        return result;
    }

    /**
     * 注册用户
     *
     * @param
     * @return
     */
    @PostMapping("/registerUser")
    @ResponseBody
    public Result registerUser(@RequestBody SysUserDO sysUser) {
        Result result = Result.ok();
        try {
            if (sysUser != null) {
                LocalDateTime now = LocalDateTime.now();
                sysUser.setSalt(String.valueOf(now.getMinute() + now.getSecond()));
                sysUser.setPassword(SecureUtil.md5(sysUser.getPassword() + sysUser.getSalt()));
                sysUserService.saveOrUpdate(sysUser);
            } else {
                return Result.error("不能为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("系统错误");
        }
        return result;
    }

}
